package com.tecode.config;

import com.tecode.intercepter.MyIntercepter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import java.util.Arrays;

/**
 * @author :刘奇
 * time:2019/4/2
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Bean
    public MyIntercepter myIntercepter(){
        return new MyIntercepter();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(myIntercepter()).addPathPatterns("/index.page").
                excludePathPatterns(Arrays.asList(
                        "/",
                        "/loginIn"
                ));
        super.addInterceptors(registry);
    }

    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/login.page");
        registry.addViewController("/index/html").setViewName("redirect:/index.page");
        registry.addViewController("/admin-role.html").setViewName("/admin-role");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        String os = System.getProperties().getProperty("os.name");
        registry.addResourceHandler("/lib/**")
                .addResourceLocations("classpath:/static/lib/");
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/static/");
        registry.addResourceHandler("/temp/**")
                .addResourceLocations("classpath:/static/temp/");
        registry.addResourceHandler("/img/**")
                .addResourceLocations("classpath:/static/img/");
        registry.addResourceHandler("/mp3/**")
                .addResourceLocations("classpath:/static/mp3/");
//        registry.addResourceHandler("/*.html")
//                .addResourceLocations("classpath:/templates/");
        if(os.toLowerCase().startsWith("win")){
            registry.addResourceHandler("/mp3/**")
                    .addResourceLocations("file:D:/");
        }else if(os.toLowerCase().startsWith("linux")){
            registry.addResourceHandler("/mp3/**")
                    .addResourceLocations("file:/");
        }
        super.addResourceHandlers(registry);
    }
}
